package com.tom.architect02.zframework.sandbox;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;


import com.tom.architect02.R;
import com.tom.architect02.zframework.sandbox.base.FileAccessFactory;
import com.tom.architect02.zframework.sandbox.base.FileRequest;
import com.tom.architect02.zframework.sandbox.base.FileResponse;
import com.tom.architect02.zframework.sandbox.base.ImageRequest;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;


@RequiresApi(api = Build.VERSION_CODES.Q)
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkPermission(this);
    }
    public static boolean checkPermission(
            Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && activity.checkSelfPermission(
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            activity.requestPermissions(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            }, 1);

        }
        return false;
    }
    @RequiresApi(api = Build.VERSION_CODES.Q)
    public void create(View view) {
        FileRequest request = new FileRequest(new File("David12"));
        request.setPath(" David12");
        request.setDisplayName("David12");
        request.setTitle("David12"); // 创建文件夹。
        FileAccessFactory.getIFile(request).newCreateFile(this, request);
    }

    public void createImg(View view) { // 创建图片(文件)。要放到对应的目录。文件管理器里面能看到图。
        ImageRequest imageRequest = new ImageRequest(new File("Maniu.jpg"));
        imageRequest.setPath("Images"); // 相对路径Pictures/Images/修改后的图片.jpg
        imageRequest.setDisplayName("修改后的图片.jpg");
        imageRequest.setMimeType("image/jpeg"); // MimeType
        FileResponse fileResponce = FileAccessFactory.getIFile(imageRequest).newCreateFile(this, imageRequest);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.die);
        try {
            OutputStream outputStream = getContentResolver().openOutputStream(fileResponce.getUri());
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Toast.makeText(this, "添加图片成功", Toast.LENGTH_SHORT).show();
    }

    // 查询图片
    public void queryFile(View view) {
//        前面是确定uri类型
        ImageRequest imageRequest = new ImageRequest(new File("修改后的图片.jpg"));
        imageRequest.setDisplayName("修改后的图片.jpg");
//        imageRequest.setPath("David"); // 指定文件夹下查图片
        FileAccessFactory.getIFile(imageRequest).query(this, imageRequest);
    }

    public void updateFile(View view) {
        // 原来的文件
        ImageRequest from = new ImageRequest(new File("修改后的图片.jpg"));
        from.setDisplayName("修改后的图片.jpg");

        // 修改后
        ImageRequest to = new ImageRequest(new File("david.jpg"));
        to.setDisplayName("David.jpg");
        FileAccessFactory.getIFile(from).renameTo(this, from, to);
    }

    public void deleteFile(View view) {
        ImageRequest where = new ImageRequest(new File("David.jpg"));
        where.setDisplayName("David.jpg");
        FileAccessFactory.getIFile(where).delete(this, where);
    }

}
